package com.atguigu.sync;

/**
 * @Author: LDeng
 * @Date: 2021-09-05 11:46
 */

class Share2{
    private int number=0;

    //+1
    public synchronized void inc() throws InterruptedException {
        if(number!=0){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        this.notifyAll();
    }

    //-1
    public synchronized void dec() throws InterruptedException {
        if(number!=1){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        this.notifyAll();
    }

}
public class ThreadDemo2 {

    public static void main(String[] args) {
        Share2 share2=new Share2();
        new Thread(()->{
            for (int i = 1; i < 10; i++) {
                try {
                    share2.inc();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 1; i < 10; i++) {
                try {
                    share2.dec();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();
    }

}
